﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fx.Data.NHibernate;
using Fx.Serialization;
using System.Diagnostics;
namespace Fx.Utility
{
    public class Logger
    {
        public static bool LogToDatabase
        {
            get
            {
                return true;
            }
        }

        public static void Log(string action, Exception ex)
        {
            Log(action, ex, null);
        }

        public static void Log(string action, Exception ex, object entity)
        {
            try
            {
                var log = CreateLogInfo(action, ex, entity);
                if (LogToDatabase)
                {
                    var contextObject = new ContextObject();                    
                    contextObject.Save(log);
                    contextObject.CommitChanges();
                }
            }
            catch (Exception ex1)
            {
                Trace.TraceError(GetMessageFromException(ex1));
                Trace.TraceError(GetStackTraceFromException(ex1));
            }
        }

        private static object CreateLogInfo(string action, Exception ex, object entity)
        {
            var log = new Fx.Mvc.Security.Entities.Log();
            log.Action = action;
            log.Application = GetCurrentApplication();
            log.Exception = GetMessageFromException(ex);
            log.StackTrace = GetStackTraceFromException(ex);
            log.Creator = GetCurrentUser();
            log.Data = GetDataJson(entity);
            log.EntryDate = DateTime.Now;
            return log;
        }

        private static string GetDataJson(object entity)
        {
            var serializer = new JsonSerializer();
            if (entity != null)
                return serializer.Serialize(entity);
            return string.Empty;
        }

        private static string GetCurrentUser()
        {
            return "need to implement";
        }

        private static string GetStackTraceFromException(Exception ex)
        {
            if (ex != null)
            {
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.InnerException != null)
                        return ex.InnerException.InnerException.StackTrace;
                    else
                        return ex.InnerException.StackTrace;
                }
                else
                    return ex.StackTrace;
            }
            return string.Empty;
        }

        private static string GetMessageFromException(Exception ex)
        {
            if (ex != null)
            {
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.InnerException != null)
                        return ex.InnerException.InnerException.Message;
                    else
                        return ex.InnerException.Message;
                }
                else
                    return ex.Message;
            }
            return string.Empty;
        }

        private static string GetCurrentApplication()
        {
            return "SBSK.Admin, need to implement";
        }
    }
}
